<?php if(!defined('OCTOMS')){header('HTTP/1.1 403');die('{"error":"forbidden"}');}
/*
 * @package       OctoMS
 * @link          http://DesignJotter.com
 * @copyright     Copyright 2012, Valentino-Jivko Radosavlevici (http://valentino.radosavlevici.com)
 * @license       GPL v3.0 (http://www.gnu.org/licenses/gpl-3.0.txt)
 * 
 * Redistributions of files must retain the above copyright notice.
 * 
 * @since         OctoMS 0.0.1
 */
	
	/*
	 * Content widget
	 * 
	 * @package OctoMS
	 * @subpackage WP-OctoMS
	 * @version 0.1
	 * 
	 * @author Valentino-Jivko Radosavlevici
	 */
	class w__content extends octoms
	{
		/*
		 * Constructor
		 */
		function __construct()
		{
			// Load the view core library
			$this->_('view');
			
			// Load the input core library
			$this->_('input');
			
			// Load the session library
			$this->_('session');
			
			// Load the template library
			$this->_('template',OMS_L);
			
			// Load this widget's model
			$this->_('w__content',OMS_M,'model');
		}
		
		/**
		 * Identity
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function identity($phone=null,$essentials=false)
		{
			// Load the main model
			$this->_('main',OMS_M,'main','island',true);
			
			// Get the available social accounts
			$accounts = $this->main->identity_accounts();
			
			// Display the social accounts
			if (is_null($phone))
			{
				// For each of them, load the icon
				if(count($data = $this->main->identity_read())>0)
				{
					if ($essentials)
					{
						print '<ul>';
							if (strlen($data['facebook'])>0) print '<li>Like us on Facebook: <a href="'.sprintf($accounts['facebook'],$data['facebook']).'">'.str_replace('http://', '', sprintf($accounts['facebook'],$data['facebook'])).'</a></li>';
							if (strlen($data['twitter'])>0) print '<li>Follow us on Twitter: <a href="'.sprintf($accounts['twitter'],$data['twitter']).'">'.str_replace('http://', '', sprintf($accounts['twitter'],$data['twitter'])).'</a></li>';
							if (strlen($data['linkedin'])>0) print '<li>Join us on LinkedIn: <a href="'.sprintf($accounts['linkedin'],$data['linkedin']).'">'.str_replace('http://', '', sprintf($accounts['linkedin'],$data['linkedin'])).'</a></li>';
							if (strlen($data['youtube'])>0) print '<li>Watch us on YouTube: <a href="'.sprintf($accounts['youtube'],$data['youtube']).'">'.str_replace('http://', '', sprintf($accounts['youtube'],$data['youtube'])).'</a></li>';
						print '</ul>';
					}
					else 
					{
						foreach ($data AS $k => $v)
						{
							if (strlen($v) != 0 && $k != 'phone')
							{
								print a($k,sprintf($accounts[$k],$v),array('class'=>$k,'target'=>'_blank'));
							}
						}
					}
				}
			}
			// Display the phone number
			else 
			{
				$data = $this->main->identity_read();
				if (isset($data['phone']) && strlen($data['phone'])>0)
				{
					print tag('span',array('class'=>'number'),$data['phone']);
				}
			}
		}// end function identity()
		
		/**
		 * Slider widget
		 * 
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function slider($theme='default')
		{
			// Load the slider images
			$s = $this->settings->get('slider');
			
			// Set the allowed effects
			$effects = (array)$this->settings->get('slider_effects');
			$allowed = array(
				'sliceDown',
				'sliceDownLeft',
				'sliceUp',
				'sliceUpLeft',
				'sliceUpDown',
				'sliceUpDownLeft',
				'fold',
				'fade',
				'slideInRight',
				'slideInLeft',
				'boxRandom',
				'boxRain',
				'boxRainReverse',
				'boxRainGrow',
				'boxRainGrowReverse'
			);
			
			// Prepare the effects
			$e = array();
			
			// Verify the effect is available
			foreach ($effects AS $key => $value)
			{
				if (in_array($key, $allowed) && $value)
				{
					$e[] = $key;
				}
			}
			
			// Merg the effects back
			$e = implode(',', $e);
			
			if (!is_null($s) && count($s)>0)
			{
				// Load the JavaScript
				$this->template->scriptroot('_content');
				$this->template
					->js('jquery.nivo.slider.pack')
					->css('nivo-slider')
					->css($theme);
				
				// Order the results
				$result = array();
				foreach($s AS $k=>$v)
				{
					$result[$v['weight']] = array(
						'id'=>$k,
						'desc'=>$v['desc'],
						'url'=>WEBROOT.'/upload/front/'.$k.'.png'
					);
				}
				ksort($result);
				
				// Load the view
				$this->view->display(
					'slider',
					array(
						'data'=>$result,
						'theme'=>$theme
					)
				);
				
				print js('jQuery(window).load(function() {
					$ = jQuery;
				    $(\'#slider\').nivoSlider({
				    	effect: \''.$e.'\',
				    	directionNavHide: false
				    });
				});',false);
			}
			
		}// end function slider()
		
		/**
		 * Post template
		 * Display a given post in The Loop
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function post($id,$position)
		{
			$this->view->display('post',
				array(
					'id'=>$id,
					'position'=>$position
				)
			);
			
		}// end function post()
		
		/**
		 * Handle the AJAX calls
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function post_ajax()
		{
			// Return an AJAX response
			if ($this->input->post('#*#'))
			{
				// Clean this attribute for the inner calls
				unset($_POST['#*#']);
				
				// Get the model method
				if ($this->input->post('action') && $this->input->post('data'))
				{
					// Clean the action
					$action = strtolower(trim($this->input->post('action','all')));

					// Verify it exists
					if (strpos($action, '_')!==0 && method_exists($this->model, $action))
					{
						// Clean the data
						if (!in_array(typeof($this->input->post('data')),array('array','object')))
						{
							$data = array($this->input->post('data','all'));
						}
						else 
						{
							$data = object_to_array($this->input->post('data','all'));
						}
						
						// Append the data to the model
						$this->model->data = $data;

						// Start the buffer
						ob_start();
						$return = call_user_func(array($this->model,$action));
						$data = ob_get_clean();
						
						// Set the response message
						if (is_array($return))
						{
							call_user_func_array(array($this->template,'_api'), $return);
						}
						elseif (is_string($return))
						{
							$this->template->_api($return);
						}
						elseif (is_bool($return))
						{
							if (!$return)
							{
								$this->template->_api('Something went wrong. Please refresh and try again.',false);
							}
						}
						
						// Append the functions
						$this->template->_api($this->model->_f());
						
						// Set the content
						$cnt = $this->model->_c();
						$cnt['data'] = $data;
						$this->template->_api_cnt($cnt);
					}
					else 
					{
						$this->template->_api('Invalid action "'.$action.'". Please refresh and try again.',false);
					}
				}
				else 
				{
					$this->template->_api('Invalid request. Please refresh and try again.',false);
				}
				
				// Prepare the API response array
				$api = array();
				
				// Set the status
				is_null($api['_status_'] = $this->template->_get1('api_status'))?($api['_status_'] = array('Done.',true)):null;
				
				// Set the filters
				$api['_filters_'] =(is_null($api['_filters_'] = $this->template->_get1('api_filters')))?array():array_flat($api['_filters_']);
				
				// Set the content
				!is_null($cnt = $this->template->_get1('api_content'))?($api['_content_']=$cnt):null;
				
				// This is a plain JSON response
				$this->template->_conf('whitescreen',true);
				while(@ob_end_clean());
				
				// We <3 JSON
				print json_encode($api);
				exit();
			}

			// Load the JS
			$this->template
				->scriptroot()
				->js('wp-octoms')
				->scriptroot('_content')
				->css('chromium')
				->js('chromium');
			
		}// end function post_ajax()
		
	}// end class w__content
	
	
/* End Of File <w__content.inc> */